package bootstrap

import (
	"com.opennews.openplatform/web_api/domain"
	"com.opennews.openplatform/web_api/services"
	"com.opennews.openplatform/web_api/shared"
)

// The function used to initialize application variables.
func init() {
	// Creates for configuration usage.
	shared.ConfigService = shared.NewAppConfigService()

	// // Initializes gorm data structure.
	initGorm()

	// Initializes request map data if needed.
	services.NewRequestMapService().Initialize()

	// Queries and caches config request map from data source.
	services.NewRequestMapService().RefreshCache()
}

// Initializes gorm data structure.
func initGorm() {
	shared.DB = shared.GetDB()

	// Migrates the schema
	shared.DB.AutoMigrate(
		&domain.RequestMap{},
		&domain.Product{},
	)

	// Batch inserting for testing only.
	//for j := 0; j < 10; j++ {
	//	products := []domain.Product{}
	//
	//	for i := 0; i < 10000; i++ {
	//		products = append(products, domain.Product{Name: "Product-" + shared.IntToString(i)})
	//	}
	//
	//	// Batch insert
	//	// To efficiently insert large number of records, pass a slice to the Create method.
	//	// GORM will generate a single SQL statement to insert all the data and backfill primary key values.
	//	db.Debug().Create(&products)
	//}
}
